package com.jxwifi.cloud.quickclean.order;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.idroid.utils.NoDoubleClickListener;
import com.jxwifi.cloud.quickclean.R;
import com.jxwifi.cloud.quickclean.app.AccountMgr;
import com.jxwifi.cloud.quickclean.app.ClientBasicActivity;
import com.jxwifi.cloud.quickclean.app.UrlPool;
import com.jxwifi.cloud.quickclean.bean.InfoBean;
import com.jxwifi.cloud.quickclean.model.AuzImpl;
import com.jxwifi.cloud.quickclean.okhttp.Params;
import com.jxwifi.cloud.quickclean.utils.TitleClient;
import com.jxwifi.cloud.quickclean.utils.ToastUtils;
import com.umeng.analytics.MobclickAgent;
import com.zhy.view.flowlayout.TagFlowLayout;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 输入备注
 * Created by Administrator on 2019/6/11.
 */

public class AddRemarksActivity extends ClientBasicActivity implements RemarksView {

    private String TAG = this.getClass().getSimpleName();
    private static String QR_CODE_BEAN = "QR_REMARKS";
    @Bind(R.id.id_flowlayout)
    TagFlowLayout mIdFlowLayout;
    @Bind(R.id.et_remarks_evaluation)
    EditText mEtRemarksEvaluation;
    @Bind(R.id.tv_add_remarks_text)
    TextView tv_add_remarks_text;
    @Bind(R.id.tv_remark)
    TextView tv_remark;
    @Bind(R.id.tv_complaint_num)
    TextView tv_complaint_num;

    private LayoutInflater mInflater;

    private AddRemarksPresenter mPresenter;
    private int creedFlag;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mInflater = LayoutInflater.from(this);
        setContentView(R.layout.activity_add_remarks);
        ButterKnife.bind(this);
        openEventBus();
        creedFlag = getIntent().getIntExtra("creedFlag",0);
        mPresenter = new AddRemarksPresenter(mBaseContext, this);
        initHead();
        mPresenter.initView();
    }

    private void initHead() {
        final TitleClient holder = new TitleClient(ButterKnife.findById(this, R.id.lin_client_title));
        if(creedFlag == 1){
            mIdFlowLayout.setVisibility(View.INVISIBLE);
            tv_add_remarks_text.setVisibility(View.INVISIBLE);

            tv_remark.setVisibility(View.INVISIBLE);

            if(!"".equals(AccountMgr.getpassword(mBaseContext)) && null != AccountMgr.getpassword(mBaseContext)){
                mEtRemarksEvaluation.setText(AccountMgr.getpassword(mBaseContext));
            }else{
                mEtRemarksEvaluation.setHint("请输入人生信条");
            }
            holder.defineTitle("人生信条");
            holder.defineTextRight("保存", new NoDoubleClickListener() {
                @Override
                protected void onNoDoubleClick(View v) {
                    if (TextUtils.isEmpty(mEtRemarksEvaluation.getText().toString())){
                        Toast toast = Toast.makeText(mBaseContext, "人生信条不能为空", Toast.LENGTH_SHORT);
                        ToastUtils.showMyToast(toast, 3000);
                        return;
                    }
                    mPresenter.saveCreed();

                }
            });
        }else {
            holder.defineTitle("输入备注");

        }
        tv_complaint_num.setText(mEtRemarksEvaluation.getText().toString().length()+"/50");
        //监听内容变化
        mEtRemarksEvaluation.addTextChangedListener(new TextWatcher(){

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                //Log.i(TAG,"beforeTextChanged ==> mEtRemarksEvaluation = "+s+" length = "+s.length());
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                //Log.i(TAG,"onTextChanged ==> mEtRemarksEvaluation = "+s+" length = "+s.length());
            }

            @Override
            public void afterTextChanged(Editable s) {
                //Log.i(TAG,"afterTextChanged ==> mEtRemarksEvaluation = "+s+" length = "+s.length());
                tv_complaint_num.setText(s.length()+"/50");

                //该设置无效
                /*if(s.equals(AccountMgr.getpassword(mBaseContext))){
                    holder.setTextRightEnable(false);
                }else{
                    holder.setTextRightEnable(true);
                }*/
            }
        });

        holder.defineLeft(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                onBackPressed();
            }
        });
    }

    @OnClick({R.id.tv_remark})
    void clickCK(View v) {
        switch (v.getId()) {
            case R.id.tv_remark:
                if (TextUtils.isEmpty(mEtRemarksEvaluation.getText().toString())){
                    Toast toast = Toast.makeText(mBaseContext, "备注信息不能为空", Toast.LENGTH_SHORT);
                    ToastUtils.showMyToast(toast, 3000);
                    return;
                }
                mPresenter.onRemarks();
                MobclickAgent.onEvent(this, "tv_remark");
                finish();
                break;
        }
    }

    @Override
    public EditText getEvaluation() {
        return mEtRemarksEvaluation;
    }

    @Override
    public void onFailure(int errorCode, String msg) {
        Toast toast = Toast.makeText(mBaseContext, msg, Toast.LENGTH_SHORT);
        ToastUtils.showMyToast(toast, 3000);
    }

    @Override
    public void onSuccess(String data) {
        InfoBean mInfoBean= JSON.parseObject(data, InfoBean.class);
        AccountMgr.putUser(mBaseContext,mInfoBean,false);
        Toast toast = Toast.makeText(mBaseContext, "修改成功", Toast.LENGTH_SHORT);
        ToastUtils.showMyToast(toast, 3000);
        finish();
    }

    @Override
    public TagFlowLayout getTagFlow() {
        return mIdFlowLayout;
    }

    @Override
    public LayoutInflater getInflater() {
        return mInflater;
    }


}
